package com.atguigu.yygh.hosp.service.impl;


import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.hosp.respository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

/**
 * @Author luoyin
 * @Date 21:29 2022/9/26
 **/
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;
    @Override
    public void saveHospital(Hospital hospital) {
        //判断是否是第一次提交
        Hospital targetHospital = hospitalRepository.getByHoscode(hospital.getHoscode());
        if(targetHospital == null){
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
        }else {
            hospital.setStatus(targetHospital.getStatus());
            hospital.setCreateTime(targetHospital.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(targetHospital.getIsDeleted());
            hospital.setId(targetHospital.getId());
        }
        hospitalRepository.save(hospital);
    }

    @Override
    public Hospital findByHoscode(String hoscode) {
        Hospital hos = new Hospital();
        hos.setHoscode(hoscode);
        Example<Hospital> example = Example.of(hos);
        Optional<Hospital> one = hospitalRepository.findOne(example);
        if (one.isPresent()){
            Hospital hospital = one.get();
            packHospital(hospital);
            return hospital;
        }

        return null;

    }

    @Override
    public Page<Hospital> selectPage(Integer pageNum, Integer pageSize, HospitalQueryVo hospitalQueryVo) {
        //查询条件
        Hospital hos = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hos);
        hos.setIsDeleted(0);
        //模糊查询
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("hosname",ExampleMatcher.GenericPropertyMatchers.contains())
                .withIgnoreCase(true);//忽略大小写
        Example<Hospital> example = Example.of(hos,matcher);
        //分页 0是第一页
        PageRequest pageRequest = PageRequest.of(pageNum - 1,pageSize,Sort.by(Sort.Direction.DESC,"createTime"));

        Page<Hospital> all = hospitalRepository.findAll(example, pageRequest);
        List<Hospital> pages = all.getContent();
        pages.forEach(this::packHospital);
        return all;
    }

    @Override
    public Hospital getHospById(String id) {
        Optional<Hospital> optional = hospitalRepository.findById(id);
        if (optional.isPresent()){
            Hospital hospital = optional.get();
            packHospital(hospital);
            return hospital;
        }
        return null;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        if(status == 0 || status == 1){
            Hospital hospital = getHospById(id);
            hospital.setStatus(status);
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {

        return hospitalRepository.getByHosnameLike(hosname);
    }

    private void packHospital(Hospital hospital) {
        //获取医院等级
        String hostype = hospital.getHostype();
        String hostypeName = dictFeignClient.getNameByParentCodeAndValue(DictEnum.HOSTYPE.getDictCode(), hostype);

        //获取 省 ,市 ,区 的名称
        String provinceCode = hospital.getProvinceCode();
        String provinceName = dictFeignClient.getNameByValue(provinceCode);
        String cityCode = hospital.getCityCode();
        String cityName = dictFeignClient.getNameByValue(cityCode);
        String districtCode = hospital.getDistrictCode();
        String districtName = dictFeignClient.getNameByValue(districtCode);
        String address = provinceName + cityName + districtName + hospital.getAddress();


        HashMap<String, Object> map = new HashMap<>();
        map.put("address",address);
        map.put("hostypeName",hostypeName);
        hospital.setParam(map);
    }
}
